package com.zzb.myshowme.model.impl;

import android.util.Log;

import com.zzb.myshowme.MyApplication;
import com.zzb.myshowme.bean.UserInfo;
import com.zzb.myshowme.model.PhoneLoginModel;
import com.zzb.myshowme.utils.AccountUtils;
import com.zzb.myshowme.utils.UserInfoUtils;
import com.zzb.myshowme.utils.HttpManager;
import com.zzb.myshowme.utils.SharedPreferencesUtil;

import rx.Subscriber;

/**
 * Created by Administrator on 2016/10/13.
 */
public class PhoneLoginModelImpl implements PhoneLoginModel {

    @Override
    public void login(final String username, final String password, final OnLoginFinishedListener listener) {
//        HttpManager.getInstance().doHttpRequest(HttpManager.getInstance().getHttpService().loginReq(username, password), new Subscriber<ResponseBody>() {
        HttpManager.getInstance().doHttpRequest(HttpManager.getInstance().getHttpService().testReq(), new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Log.d("upload", "onCompleted down 成功======");

            }

            @Override
            public void onError(Throwable e) {
                Log.d("down", "onError 上传头像======" + e.getMessage());
                listener.onFailure("网络连接失败，请查看网络设置");
            }

            @Override
            public void onNext(String string) {
                UserInfo user = new UserInfo();
                user.setUser("13528719585");
                user.setSex("男");
                user.setNickName("ZZB");
                UserInfoUtils.writeLoginMember(MyApplication.getInstance(), user);//保存到文件中
                SharedPreferencesUtil.setBoolean(MyApplication.getInstance(), SharedPreferencesUtil.LOGIN, true);//设置登录状态
                AccountUtils.setPassword(MyApplication.getInstance(), password);
                AccountUtils.setUserId(MyApplication.getInstance(), username);
                listener.onSuccess(user);
            }
        });
    }
}
